package com.example.anliku.service.serviceImpl;

import com.example.anliku.Utils.RedisUtil;
import com.example.anliku.entity.Download;
import com.example.anliku.entity.Message;
import com.example.anliku.mapper.*;
import com.example.anliku.service.StatisticsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private TeamMapper teamMapper;

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private ExampleMapper exampleMapper;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private DownloadMapper downloadMapper;

    @Override
    public Message StatisticsAll(){
        HashMap map = new HashMap();
        map.put("userNum", userMapper.selectALL().size());
        map.put("teamNum", teamMapper.selectAll().size());
        map.put("adminNum", adminMapper.selectAll().size());
        map.put("commentNum", commentMapper.selectAll().size());
        map.put("exampletNum", exampleMapper.selectAll().size());
        map.put("downloadtNum", downloadMapper.selectAll().size());
        return Message.success(null).add(map);
    }

    @Override
    public Message clazzAndKno(String year) {
        if(year!=null){
            List<Integer> s1 = new LinkedList<>();
            for(int i=1;i<=12;i++){
                String date=year+"-"+i;
                Integer s = downloadMapper.selectAllByDownloadTime(date);
                s1.add(s);
            }
            return Message.success(null).add(s1);
        }else {
            return Message.fail(null);
        }

    }

    @Override
    public Message selectBig() {

        List<Map> ss = exampleMapper.selectByAll();

     return Message.success(null).add(ss);
    }


    @Override
    public Message DownloadByCaseId(Integer exampleId) {
        boolean rs1 = redisUtil.hasKey("download" + exampleId);
        HashMap map = new HashMap();
        if(!rs1){
            map.put("downloadtNum",0);
        }else{
            map.put("downloadtNum",redisUtil.get("download"+exampleId));
        }
        return Message.success(null).add(map);
    }

    private static List<String> getDaysBetwwen(int days){ //最近几天日期
        List<String> dayss = new ArrayList<>();
        Calendar start = Calendar.getInstance();
        start.setTime(getDateAdd(days));
        Long startTIme = start.getTimeInMillis();
        Calendar end = Calendar.getInstance();
        end.setTime(new Date());
        Long endTime = end.getTimeInMillis();
        Long oneDay = 1000 * 60 * 60 * 24L;
        Long time = startTIme;
        while (time <= endTime) {
            Date d = new Date(time);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            dayss.add(df.format(d));
            time += oneDay;
        }
        return dayss;
    }

    private static  Date getDateAdd(int days){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -days);
        return c.getTime();
    }
}
